<?php

class PhpFox_Mod_Video_ComponentApproveVideos extends BaseComponent
{
    function process()
    {
    	$oSrvVideo = &$this->_oModule->getService('Video');
    	$oReq = &App::getRequest();
    	$oUrl = &App::getUrl();

    	$iPageSize = 10;

        if (!$iPageSize)
        {
            $iPage = 0;
            $oUrl->clearParams('page');
        }
        else
        {
            $iPage = ($this->getParam('page') ? $this->getParam('page')  : $oReq->getInt('page', 1));
        }

    	App::loadClass('Pager');

		if ( $iDeleteVideo = $oReq->get('dvideo') )
		{
			$bIsDeleted = $oSrvVideo->deletePendingVideo($iDeleteVideo);
		}

		if ( $iApproveVideo = $oReq->get('avideo') )
		{
			$sDoApprove = $oSrvVideo->approveVideo($iApproveVideo);
		}

    	$aGetFields = $oReq->getArray('sel_videos');

    	if ( count($aGetFields) > 0 )
    	{
    		$sDoApprove = $oReq->get('approve');

    		foreach($aGetFields as $aTempId)
    		{
    			($sDoApprove ? $oSrvVideo->approveVideo($aTempId) : $oSrvVideo->deletePendingVideo($aTempId));
    		}

    		if ( !$sDoApprove )
    		{
    			$bIsDeleted = true;
    			unset($sDoApprove);
    		}
    	}

        $iCnt = $this->oDatabase->getField("SELECT COUNT(*) FROM ". App::getT('videos_pending') ." AS v");

		$iOffset = $this->oDatabase->_getPageNum($iPage, $iPageSize, $iCnt);

   		$aItems = $this->oDatabase->getRows("SELECT v.*, u.user FROM
   			". App::getT('videos_pending') ." AS v
   			LEFT JOIN ". App::getT('user') ." AS u ON(v.vid_userid = u.id)
   			ORDER BY v.vid_time LIMIT ". $iOffset .",". $iPageSize ."");

		$oPager = &new Pager($iCnt, $iPage, $iPageSize);
		$iFrom = $oPager->getOffset($iPage, $iPageSize);
		$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

		$this->oTpl->assign(array
		(
			'bApprovalSetting' => App::getParam('vid_approve'),
			'bIsDeleted' => (isset($bIsDeleted) ? true : false),
			'sDoApprove' => (isset($sDoApprove) ? true : false),
			'sGImageUrl' => App::getParam('video.image.url'),
			'aItems' => $aItems,
        	'iCnt'             => $iCnt,
        	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
			'aPaging'          => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        	'iTo'              => $iTo,
        	'sGImageUrl' => App::getParam('video.image.url'),
		));

		return $this->oTpl->fetch('ApproveVideos.html');
    }
}

?>